/**
 * 消息记录管理初始化
 */
var VpnPushMsg = {
    id: "VpnPushMsgTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    form: null
};

/**
 * 初始化表格的列
 */
VpnPushMsg.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: '序号', field: 'id', visible: true, align: 'center', valign: 'middle'},
            {title: '标题', field: 'title', visible: true, align: 'center', valign: 'middle'},
            {title: '内容', field: 'content', visible: true, align: 'center', valign: 'middle'},
            // {title: '类别', field: 'category', visible: true, align: 'center', valign: 'middle'},
            // {title: '做操人ID', field: 'operationUserId', visible: true, align: 'center', valign: 'middle'},
            // {title: '创建时间', field: 'createTime', visible: true, align: 'center', valign: 'middle'},
            {title: '发送时间', field: 'sendTime', visible: true, align: 'center', valign: 'middle'},
            // {title: '信息更新时间', field: 'updateTime', visible: true, align: 'center', valign: 'middle'},
            {title: '状态', field: 'status',
                visible: true, align: 'center', valign: 'middle',formatter: function (value, row, index) {
                    if(value == '1'){
                        return "已发送"
                    }
                    else if(value == "2"){
                        return "发送失败"
                    }
                    else if(value == "0"){
                        return "未发送"
                    }
                }},
            {title: '操作', field: '',
            visible: true, align: 'center', valign: 'middle',formatter: function (value, row, index) {
                var content = row.content;
                return "<a onclick=\"preview('"+content.split('\n')+"')\">预览消息</a>";
            }}
    ];
};

function preview(row){
    var c = row.split(",");
    const len = c.length;
    var str="";
    for (let i = 0; i < len; i++) {
        str += c[i] + "<br/>"
    }
    var content = '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">'+str+'</div>';
    layer.open({
        type: 1
        ,title: false //不显示标题栏
        ,closeBtn: false
        ,area: '500px;'
        ,shade: 0.8
        ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
        ,btn: ['关闭']
        ,btnAlign: 'c'
        ,moveType: 1 //拖拽模式，0或者1
        ,content: content
        ,success: function(layero){

        }
    });
}

VpnPushMsg.pushMsg = function(msgId){
    var loading;
    layer.confirm("是否想要立即推送信息",function (index) {
        layer.close(index);
        loading = layer.load(0,{
            shade:false,
            time: 30 * 1000
        });
        var ajax = new $ax(Feng.ctxPath + "/vpnPushMsg/pushMsg", function (data) {
            Feng.success("发送成功!");
            VpnPushMsg.table.refresh();
            layer.close(loading);
        }, function (data) {
            Feng.error(data.responseJSON.message + "!");
            layer.close(loading);
        });
        ajax.set("msgId",msgId);
        ajax.start();
    });
};

/**
 * 检查是否选中
 */
VpnPushMsg.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        VpnPushMsg.seItem = selected[0];
        return true;
    }
};

// VpnPushMsg.initForm = function(){
//     layui.use('form', function(){
//         var form = layui.form;
//         VpnPushMsg.form = form;
//
//         //监听提交
//         form.on('submit(formDemo)', function(data){
//             var ajax = new $ax(Feng.ctxPath + "/vpnPushMsg/addPushMsg", function (data) {
//                 Feng.success("添加成功!");
//             }, function (data) {
//                 Feng.error("添加失败!" + data.responseJSON.message + "!");
//             });
//             ajax.set(data.field);
//             ajax.start();
//             return false;
//         });
//     });
// }

// VpnPushMsg.clearForm = function(){
//     var emptyForm = VpnPushMsg.emptyForm();
//     VpnPushMsg.form.val("layuiadmin-app-form-list", emptyForm);
// };
//
// VpnPushMsg.emptyForm = function(){
//     var elms = $("#layuiadmin-app-form-list [name]"); //formid 包含name属性的所有元素
//     var obj = {};
//     $.each(elms, function (i, item) {
//         var name = $(item).attr("name");
//         obj[name] = "";
//     });
//     return obj;
// };

$(function () {
    var defaultColunms = VpnPushMsg.initColumn();
    var table = new BSTable(VpnPushMsg.id, "/vpnPushMsg/list", defaultColunms);
    table.setPaginationType("client");
    VpnPushMsg.table = table.init();
});
